


kernel void bgr2z(read_only image2d_t src,write_only image2d_t dst)  
{  
	const sampler_t sampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST ; 
    int x = (int)get_global_id(0);  
    int y = (int)get_global_id(1);  
    if (x >= get_image_width(src) || y >= get_image_height(src))  
        return;  
    
    float3 pixel_=read_imagef(src, sampler, (int2)(x, y)).xyz;
   
    float r,g,b,Z;
	float uniformS = 1.0/255.0;
	float smallS = 1.0/12.0;
	float largeAdder = 0.055/1.055;
	float largeS = 1.0/1.055;
    
    float dst_=0.11*pixel_.x+0.59*pixel_.y+0.30*pixel_.z;
    b = pixel_.x; g = pixel_.y; r = pixel_.z ;
   
	if (r <= 0.04045)
	{
		r = r * smallS;
	}
	else
	{
		r = exp2(2.4 * log2(r * largeS + largeAdder));
		if (g <= 0.04045)
		{
			g = g * smallS;
		}
		else
		{
			g = exp2(2.4 * log2(g * largeS + largeAdder));
			if (b <= 0.04045)
			{
				b = b * smallS;
			}
			else
			{
				b = exp2(2.4 * log2(b * largeS + largeAdder));
			}
		}
	}
	Z = 0.019334 * r + 0.119193 * g + 0.950527 * b;
//	Z = Z*255;
    
    write_imagef(dst,(int2)(x,y),(float4)(Z,Z,Z,1.0f));  
}  